﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Operations;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class ConfigTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase("key", "value")]
        public void Can_CRUD(string key, string value)
        {
            var setting = Processor.Execute(new CreateConfigParam
            {
                Key = key,
                Value = value
            }).Result;

            Assert.NotNull(setting);
            setting.Key.AreEqualIgnoringCase(key);
            setting.Value.AreEqualIgnoringCase(value);
            setting.AreEqual(Processor.Execute(new GetConfigParam
            {
                EntityID = setting.EntityID
            }).Result);
            setting.AreEqual(Processor.Execute(new GetConfigParamByKey
            {
                Key = key
            }).Result);

            value = value.Reverse();
            var upd_setting = Processor.Execute(new UpdateConfigParam
            {
                EntityID = setting.EntityID,
                Value = value
            }).Result;

            Assert.NotNull(upd_setting);
            upd_setting.AreEqual(setting);
            upd_setting.Key.AreEqualIgnoringCase(key);
            upd_setting.Value.AreEqualIgnoringCase(value);

            Processor.Execute(new DeleteConfigParam
            {
                EntityID = setting.EntityID
            });
        }

        [TestCase("key", "value")]
        public void Can_SetParam(string key, string value)
        {
            var setting = Processor.Execute(new SetConfigParam
            {
                Key = key,
                Value = value
            }).Result;

            Assert.NotNull(setting);
            setting.Key.AreEqualIgnoringCase(key);
            setting.Value.AreEqualIgnoringCase(value);

            value = value.Reverse();
            setting = Processor.Execute(new SetConfigParam
            {
                Key = key,
                Value = value
            }).Result;

            Assert.NotNull(setting);
            setting.Key.AreEqualIgnoringCase(key);
            setting.Value.AreEqualIgnoringCase(value);
        }

        [TestCase("key", "value")]
        public void Can_GetValByKey(string key, string value)
        {
            var setting = Processor.Execute(new SetConfigParam
            {
                Key = key,
                Value = value
            }).Result;

            Assert.NotNull(setting);

            string val = Processor.Execute(new GetConfigParamValueByKey
            {
                Key = key
            }).Result;

            val.AreEqualIgnoringCase(value);
        }

        [TestCase("key1", "value1", "key2", "value2", "key3", "value3")]
        public void Can_BulkUpdate(string key1, 
            string value1, 
            string key2, 
            string value2, 
            string key3, 
            string value3)
        {
            Processor.Execute(new SetConfigParam
            {
                Key = key1,
                Value = value1
            });
            Processor.Execute(new SetConfigParam
            {
                Key = key2,
                Value = value2
            });
            Processor.Execute(new SetConfigParam
            {
                Key = key3,
                Value = value3
            });

            value1 = value1.Reverse();
            value2 = value2.Reverse();
            value3 = value3.Reverse();
            Processor.Execute(new BulkUpdateConfigParams
            {
                Settings = new Dictionary<String, String>
                {
                    {key1, value1},
                    {key2, value2},
                    {key3, value3}
                }
            });

            string val1 = Processor.Execute(new GetConfigParamValueByKey
            {
                Key = key1
            }).Result;
            string val2 = Processor.Execute(new GetConfigParamValueByKey
            {
                Key = key2
            }).Result;
            string val3 = Processor.Execute(new GetConfigParamValueByKey
            {
                Key = key3
            }).Result;

            value1.AreEqualIgnoringCase(val1);
            value2.AreEqualIgnoringCase(val2);
            value3.AreEqualIgnoringCase(val3);
        }

        [Test]
        public void Cannot_Create_If_Key_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateConfigParam
            {
                Key = String.Empty
            }));
        }
    }
}
